﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ESRP
{
    public partial class CopiazaAnaliticeFrm : FrmRezumat
    {
        public CopiazaAnaliticeFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
            conturiDataGridView.m_AutoCompletionColumns.Add(ContDestColumn);
        }

        private void CopiazaAnaliticeFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            this.unitatiTableAdapter.Connection = con;
            this.unitatiTableAdapter.Fill(this.eSRPDataSet.Unitati);
            this.conturiTableAdapter.Connection = con;
            this.conturiTableAdapter.FillAll(this.eSRPDataSet.Conturi);

            conturiBindingSource.Filter = "Terminal=1" ;
            conturiBindingSourceDest.Filter = "CodUnitate=" + ModuleInfo.CodUnitate.ToString();
            verificaContBindingSource.Filter = "CodUnitate=" + ModuleInfo.CodUnitate.ToString();
            InchideConexiune(con);

            AddButton("Copiaza", global::ERPStudio.Properties.Resources._001_07, StartCopy, ToolStripItemAlignment.Right, false);
        }


        private void StartCopy(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            String comanda = String.Empty;
            String contDest = String.Empty;
            SqlCommand cmd = null;
            foreach(DataGridViewRow rand in conturiDataGridView.Rows)
            {
                if (rand.Cells[ContDestColumn.Index].Value != null && !String.IsNullOrEmpty(rand.Cells[ContDestColumn.Index].Value.ToString())) 
                { 
                    contDest = rand.Cells[ContDestColumn.Index].Value.ToString();
                    int poz =verificaContBindingSource.Find("Cont",contDest);
                    if(poz==-1)
                        continue;
                    verificaContBindingSource.Position = poz;
                    ESRPDataSet.ConturiRow contVerif = ((DataRowView)verificaContBindingSource.Current).Row as ESRPDataSet.ConturiRow;
                    if(contVerif==null)
                        continue;

                    rand.Cells[RezultatColumn.Index].Value = contVerif.Terminal.ToString();

                    SqlTransaction tran = con.BeginTransaction();

                    if (contVerif.Terminal)
                    {
                        cmd = new SqlCommand("UPDATE [Conturi] SET Terminal = 0 WHERE [CodUnitate] = " + ModuleInfo.CodUnitate.ToString() +
                         " AND Cont='" + contDest + "'", con);
                        cmd.Transaction = tran;
                        try
                        {
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            rand.Cells[RezultatColumn.Index].Value = ex.Message;
                            continue;
                        }
                    }
                    
                    ESRPDataSet.ConturiRow contSursa = ((DataRowView)rand.DataBoundItem).Row as ESRPDataSet.ConturiRow;
                    String deinlocuit = String.Empty;
                    if (contSursa.ContParinte != "N/A")
                        deinlocuit = contSursa.ContParinte;

                    comanda = "INSERT INTO [Conturi] ([Cont]" +
                               ",[CodUnitate],[Denumire],[Tip],[Terminal],[ContParinte],[EsteAnalitic],[DebitInitial],[CreditInitial]" +
                               ",[DebitPrecedent],[CreditPrecedent],[Categorie],[RulajDebit],[RulajCredit]) VALUES ('" +
                               contDest + (deinlocuit != String.Empty ? contSursa.Cont.Replace(deinlocuit, String.Empty) :"."+contSursa.Cont) + "'," + ModuleInfo.CodUnitate.ToString() + ",'" + contSursa.Denumire + "','" +
                         contSursa.Tip + "',1,'" + contDest + "',1," + (CopiazaSoldurileInitialeCB.Checked ? contSursa.DebitInitial.ToString() + "," +
                         contSursa.CreditInitial.ToString() + "," : "0,0,") + "0,0," + contSursa.Categorie.ToString() + ",0,0)";

                    cmd = new SqlCommand(comanda, con);
                    cmd.Transaction = tran;
                    try
                    {    
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        rand.Cells[RezultatColumn.Index].Value = ex.Message;
                        continue;
                    }
                    tran.Commit();
                }
                    
            }
        }

    }
}
